package com.molyfun.parents.im.chatrow;

import android.content.Context;
import android.text.TextUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.ImageViewerActivity;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.im.model.NormalFileMessageBody;
import com.molyfun.parents.rest.QiniuSDK;
import com.molyfun.parents.utils.StringUtils;
import com.squareup.picasso.Picasso;

public class ChatRowImage extends ChatRowFile{

    protected ImageView imageView;
    private NormalFileMessageBody imgBody;
    private TextView mNickName;
    private boolean mShowNickName;

    public ChatRowImage(Context context, ChatMessageEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    public ChatRowImage(Context context, ChatMessageEntity message, int position, boolean showNickName, BaseAdapter adapter) {
        super(context, message, position, adapter);
        mShowNickName = showNickName;
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ? R.layout.ease_row_received_picture : R.layout.ease_row_sent_picture, this);
    }

    @Override
    protected void onFindViewById() {
        percentageView = (TextView) findViewById(R.id.percentage);
        imageView = (ImageView) findViewById(R.id.image);
        mNickName = (TextView) findViewById(R.id.tv_userid);
    }

    
    @Override
    protected void onSetUpView() {
        imgBody = message.getBody();
        // received messages
//        if (message.direct() == EMMessage.Direct.RECEIVE) {
        if(message.isUseNative()){
            if(message.getErrorNativeBody() != null && !TextUtils.isEmpty(message.getErrorNativeBody().getFilePath())){
                Picasso.with(context)
                        .load("file://" + message.getErrorNativeBody().getFilePath())
                        .placeholder(R.mipmap.ic_main_side_header_bg)
                        .into(imageView);
            }else{
                imageView.setImageResource(R.drawable.ease_default_image);
            }

        }else{
            if(!TextUtils.isEmpty(imgBody.getRemoteUrl())){
                Picasso.with(context)
                        .load(QiniuSDK.getBigUrl(imgBody.getRemoteUrl()))
                        .placeholder(R.drawable.ease_default_image)
                        .into(imageView);
            }else{
                imageView.setImageResource(R.drawable.ease_default_image);
            }
            return;
        }

        if(mShowNickName) {
            if(message.direct() == EMMessage.Direct.RECEIVE){
                mNickName.setText(StringUtils.getSpltString(message.getUsername(), 10));
            }else{
                mNickName.setText("[" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getStudentname(), 10) + "]" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getUsername(), 10));
            }
        }
//        }
//
//        Picasso.with(context)
//                .load("file://" + imgBody.getLocalUrl())
//                .placeholder(R.mipmap.ic_main_side_header_bg)
//                .into(imageView);
    }
    
    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }
    
    @Override
    protected void onBubbleClick() {
        if(!message.isUseNative()){
            ImageViewerActivity.startActivity(context, QiniuSDK.getBigUrl(imgBody.getRemoteUrl()));
        }
    }

}
